package com.yiyahui.project.erp.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.common.utils.DateUtils;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.project.erp.domain.YiyaAuditDiscount;
import com.yiyahui.project.erp.domain.YiyaClosingWaterRelation;
import com.yiyahui.project.erp.domain.YiyaMainClosingEntries;
import com.yiyahui.project.erp.domain.requestVo.AgainBillingRequest;
import com.yiyahui.project.erp.domain.requestVo.ChargeRecordRequest;
import com.yiyahui.project.erp.domain.requestVo.CreateMainAndItemRequest;
import com.yiyahui.project.erp.domain.requestVo.YiyaItemBillingExtend;
import com.yiyahui.project.erp.domain.responseVo.ClinicPersonEntriesVo;
import com.yiyahui.project.erp.domain.responseVo.ListMainClosingEntriesVo;
import com.yiyahui.project.erp.service.*;
import com.yiyahui.project.system.domain.SysDept;
import com.yiyahui.project.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiyahui.project.erp.mapper.YiyaBillingMainMapper;
import com.yiyahui.project.erp.domain.YiyaBillingMain;
import org.springframework.transaction.annotation.Transactional;

/**
 * 主单Service业务层处理
 * 
 * @author yiyahui
 * @date 2020-06-10
 */
@Service
public class YiyaBillingMainServiceImpl implements IYiyaBillingMainService 
{
    @Autowired
    private YiyaBillingMainMapper yiyaBillingMainMapper;

    @Autowired
    private IYiyaItemBillingService yiyaItemBillingService;

    @Autowired
    private IYiyaAuditDiscountService yiyaAuditDiscountService;

    @Autowired
    private IYiyaClosingWaterRelationService yiyaClosingWaterRelationService;

    @Autowired
    private IYiyaMainClosingEntriesService yiyaMainClosingEntriesService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IYiyaIDService yiyaIDService;

    /**
     * 查询主单
     * 
     * @param id 主单ID
     * @return 主单
     */
    @Override
    public YiyaBillingMain selectYiyaBillingMainById(Long id)
    {
        return yiyaBillingMainMapper.selectYiyaBillingMainById(id);
    }

    /**
     * 查询主单列表
     * 
     * @param yiyaBillingMain 主单
     * @return 主单
     */
    @Override
    public List<YiyaBillingMain> selectYiyaBillingMainList(YiyaBillingMain yiyaBillingMain)
    {
        return yiyaBillingMainMapper.selectYiyaBillingMainList(yiyaBillingMain);
    }

    /**
     * 新增主单
     * 
     * @param yiyaBillingMain 主单
     * @return 结果
     */
    @Override
    public int insertYiyaBillingMain(YiyaBillingMain yiyaBillingMain)
    {
        yiyaBillingMain.setCreateTime(DateUtils.getNowDate());
        return yiyaBillingMainMapper.insertYiyaBillingMain(yiyaBillingMain);
    }

    /**
     * 修改主单
     * 
     * @param yiyaBillingMain 主单
     * @return 结果
     */
    @Override
    public int updateYiyaBillingMain(YiyaBillingMain yiyaBillingMain)
    {
        yiyaBillingMain.setUpdateTime(DateUtils.getNowDate());
        return yiyaBillingMainMapper.updateYiyaBillingMain(yiyaBillingMain);
    }

    /**
     * 批量删除主单
     * 
     * @param ids 需要删除的主单ID
     * @return 结果
     */
    @Override
    public int deleteYiyaBillingMainByIds(Long[] ids)
    {
        return yiyaBillingMainMapper.deleteYiyaBillingMainByIds(ids);
    }

    /**
     * 删除主单信息
     * 
     * @param id 主单ID
     * @return 结果
     */
    @Override
    public int deleteYiyaBillingMainById(Long id)
    {
        return yiyaBillingMainMapper.deleteYiyaBillingMainById(id);
    }

    @Transactional(rollbackFor=Exception.class)
    public AjaxResult createMainAndItem(CreateMainAndItemRequest createMainAndItemRequest) {
        YiyaBillingMain yiyaBillingMain = createMainAndItemRequest.getYiyaBillingMain();
        List<YiyaItemBillingExtend> yiyaItemBillingExtends = createMainAndItemRequest.getYiyaItemBillingExtends();
        YiyaMainClosingEntries yiyaMainClosingEntries = new YiyaMainClosingEntries();
        yiyaMainClosingEntries.setClinicPersonId(yiyaBillingMain.getClinicPersonId());
        yiyaMainClosingEntries.setBillingPerson(createMainAndItemRequest.getBillingPerson());
        yiyaMainClosingEntries.setThisPay(createMainAndItemRequest.getThisPay());
        yiyaMainClosingEntries.setArrearageChange(createMainAndItemRequest.getArrearageChange());
        yiyaMainClosingEntries.setPersonArrearage(createMainAndItemRequest.getPersonArrearage());
        yiyaMainClosingEntries.setBillingRemarks(createMainAndItemRequest.getBillingRemarks());
        yiyaMainClosingEntries.setType(1); //只能是项目开单
        yiyaMainClosingEntries.setBillStatus(createMainAndItemRequest.getOneAudit() || createMainAndItemRequest.getTwoAudit() ? 2 : 1);
        if (yiyaBillingMain != null) {
            yiyaBillingMain.setOwingAmount(yiyaBillingMain.getReceivable());
            if (yiyaBillingMain.getId() != null && yiyaBillingMain.getId() > 0) { //修改
                yiyaBillingMainMapper.updateYiyaBillingMain(yiyaBillingMain);
                YiyaMainClosingEntries yiyaMainClosingEntriesRequest = new YiyaMainClosingEntries(); // 主单id+患者id 查开单流水 只会有一条记录
                yiyaMainClosingEntriesRequest.setClinicPersonId(yiyaBillingMain.getClinicPersonId());
                yiyaMainClosingEntriesRequest.setBillingMainId(yiyaBillingMain.getId());
                List<YiyaMainClosingEntries> yiyaMainClosingEntriesResponse = yiyaMainClosingEntriesService.selectYiyaMainClosingEntriesList(yiyaMainClosingEntriesRequest);
                YiyaMainClosingEntries yiyaMainClosingEntrieResponse = yiyaMainClosingEntriesResponse.get(0);
                yiyaMainClosingEntries.setId(yiyaMainClosingEntrieResponse.getId());
                yiyaMainClosingEntriesService.updateYiyaMainClosingEntries(yiyaMainClosingEntries);
                if (yiyaItemBillingExtends != null && yiyaItemBillingExtends.size() > 0) {
                    yiyaItemBillingExtends.forEach(r -> {
                        if (r.getId() != null) {
                            yiyaItemBillingService.updateYiyaItemBilling(r);
                            YiyaClosingWaterRelation yiyaClosingWaterRelationRequest = new YiyaClosingWaterRelation();
                            yiyaClosingWaterRelationRequest.setMainClosingEntriesId(yiyaMainClosingEntrieResponse.getId());
                            yiyaClosingWaterRelationRequest.setItemBillingId(r.getId());
                            List<YiyaClosingWaterRelation> yiyaClosingWaterRelationsResponse = yiyaClosingWaterRelationService.selectYiyaClosingWaterRelationList(yiyaClosingWaterRelationRequest);
                            YiyaClosingWaterRelation yiyaClosingWaterRelationResponse = yiyaClosingWaterRelationsResponse.get(0);
                            yiyaClosingWaterRelationResponse.setStoredDeductPrice(r.getStoredDeductPrice());
                            yiyaClosingWaterRelationResponse.setThisPay(r.getThisPay());
                            yiyaClosingWaterRelationService.updateYiyaClosingWaterRelation(yiyaClosingWaterRelationResponse);
                        }
                    });
                    if (createMainAndItemRequest.getOneAudit() != null || createMainAndItemRequest.getTwoAudit() != null) {
                        if (createMainAndItemRequest.getOneAudit() || createMainAndItemRequest.getTwoAudit()) {
                            if (!"".equals(createMainAndItemRequest.getClinic()) && !"".equals(createMainAndItemRequest.getProposer())) {
                                YiyaAuditDiscount yiyaAuditDiscountRequest = new YiyaAuditDiscount();
                                yiyaAuditDiscountRequest.setBillingMainId(yiyaBillingMain.getId());
                                List<YiyaAuditDiscount> yiyaAuditDiscountsResponse = yiyaAuditDiscountService.selectYiyaAuditDiscountList(yiyaAuditDiscountRequest);
                                SysDept sysDept1 = sysDeptService.selectDeptById(createMainAndItemRequest.getClinicId() != null ? createMainAndItemRequest.getClinicId() : 0);
                                SysDept sysDept2 = sysDeptService.selectDeptById(sysDept1 != null ? sysDept1.getParentId() : 0);
                                if (yiyaAuditDiscountsResponse.size() == 1) { //更新审核数据 //todo 差申请单号
                                    YiyaAuditDiscount yiyaAuditDiscountResponse = yiyaAuditDiscountsResponse.get(0);
                                    yiyaAuditDiscountResponse.setClinic(createMainAndItemRequest.getClinic());
                                    yiyaAuditDiscountResponse.setProposer(createMainAndItemRequest.getProposer());
                                    if (createMainAndItemRequest.getTwoAudit() != null && createMainAndItemRequest.getTwoAudit()) { //需要2级审核
                                        yiyaAuditDiscountResponse.setStatus(2); //待审核2
                                        yiyaAuditDiscountResponse.setAuditerOne(sysDept1.getLeader());
                                        yiyaAuditDiscountResponse.setAuditerOneResult(1); //待审核
                                        yiyaAuditDiscountResponse.setAuditerTwo(sysDept2.getLeader());
                                        yiyaAuditDiscountResponse.setAuditerTwoResult(1); //待审核
                                    } else if (createMainAndItemRequest.getOneAudit() != null && createMainAndItemRequest.getOneAudit()) { //需要1级审核
                                        yiyaAuditDiscountResponse.setStatus(1); // 待审核1
                                        yiyaAuditDiscountResponse.setAuditerOne(sysDept1.getLeader());
                                        yiyaAuditDiscountResponse.setAuditerOneResult(1); //待审核
                                    }
                                    yiyaAuditDiscountService.updateYiyaAuditDiscount(yiyaAuditDiscountResponse);
                                } else { //新建审核数据
                                    YiyaAuditDiscount yiyaAuditDiscount = new YiyaAuditDiscount();
                                    yiyaAuditDiscount.setBillingMainId(yiyaBillingMain.getId());
                                    yiyaAuditDiscount.setClinic(createMainAndItemRequest.getClinic());
                                    yiyaAuditDiscount.setProposer(createMainAndItemRequest.getProposer());
                                    if (createMainAndItemRequest.getTwoAudit() != null && createMainAndItemRequest.getTwoAudit()) { //需要2级审核
                                        yiyaAuditDiscount.setStatus(2); //待审核2
                                        yiyaAuditDiscount.setAuditerOne(sysDept1.getLeader());
                                        yiyaAuditDiscount.setAuditerOneResult(1); //待审核
                                        yiyaAuditDiscount.setAuditerTwo(sysDept2.getLeader());
                                        yiyaAuditDiscount.setAuditerTwoResult(1); //待审核
                                    } else if (createMainAndItemRequest.getOneAudit() != null && createMainAndItemRequest.getOneAudit()) { //需要1级审核
                                        yiyaAuditDiscount.setStatus(1); // 待审核1
                                        yiyaAuditDiscount.setAuditerOne(sysDept1.getLeader());
                                        yiyaAuditDiscount.setAuditerOneResult(1); //待审核
                                    }
                                    yiyaAuditDiscountService.insertYiyaAuditDiscount(yiyaAuditDiscount);
                                }
                            } else {
                                return AjaxResult.error();
                            }
                        }
                    } else {
                        return AjaxResult.error();
                    }
                } else {
                    return AjaxResult.error();
                }
            } else { //新增
                yiyaBillingMainMapper.insertYiyaBillingMain(yiyaBillingMain);
                yiyaMainClosingEntries.setBillingMainId(yiyaBillingMain.getId());
                yiyaMainClosingEntries.setDocumentNum(yiyaIDService.createYiyaID(IYiyaIDService.IDType_JIEZHANGDAN,yiyaBillingMain.getClinic())); // 单据号
                yiyaMainClosingEntriesService.insertYiyaMainClosingEntries(yiyaMainClosingEntries);
                if (yiyaItemBillingExtends != null && yiyaItemBillingExtends.size() > 0) {
                    yiyaItemBillingExtends.forEach(r -> {
                        r.setBillingMainId(yiyaBillingMain.getId());
                        yiyaItemBillingService.insertYiyaItemBilling(r);
                        YiyaClosingWaterRelation yiyaClosingWaterRelation = new YiyaClosingWaterRelation();
                        yiyaClosingWaterRelation.setItemBillingId(r.getId());
                        yiyaClosingWaterRelation.setMainClosingEntriesId(yiyaMainClosingEntries.getId());
                        yiyaClosingWaterRelation.setStoredDeductPrice(r.getStoredDeductPrice());
                        yiyaClosingWaterRelation.setThisPay(r.getThisPay());
                        yiyaClosingWaterRelationService.insertYiyaClosingWaterRelation(yiyaClosingWaterRelation);
                    });
                    if (createMainAndItemRequest.getOneAudit() != null || createMainAndItemRequest.getTwoAudit() != null) {
                        SysDept sysDept1 = sysDeptService.selectDeptById(createMainAndItemRequest.getClinicId() != null ? createMainAndItemRequest.getClinicId() : 0);
                        SysDept sysDept2 = sysDeptService.selectDeptById(sysDept1 != null ? sysDept1.getParentId() : 0);
                        if (createMainAndItemRequest.getOneAudit() || createMainAndItemRequest.getTwoAudit()) {
                            if (!"".equals(createMainAndItemRequest.getClinic()) && !"".equals(createMainAndItemRequest.getProposer())) {
                                YiyaAuditDiscount yiyaAuditDiscount = new YiyaAuditDiscount();
                                yiyaAuditDiscount.setBillingMainId(yiyaBillingMain.getId());
                                yiyaAuditDiscount.setClinic(createMainAndItemRequest.getClinic());
                                yiyaAuditDiscount.setProposer(createMainAndItemRequest.getProposer());
                                if (createMainAndItemRequest.getTwoAudit() != null && createMainAndItemRequest.getTwoAudit()) { //需要2级审核
                                    yiyaAuditDiscount.setStatus(2); //待审核2
                                    yiyaAuditDiscount.setAuditerOne(sysDept1.getLeader());
                                    yiyaAuditDiscount.setAuditerOneResult(1); //待审核
                                    yiyaAuditDiscount.setAuditerTwo(sysDept2.getLeader());
                                    yiyaAuditDiscount.setAuditerTwoResult(1); //待审核
                                } else if (createMainAndItemRequest.getOneAudit() != null && createMainAndItemRequest.getOneAudit()) { //需要1级审核
                                    yiyaAuditDiscount.setStatus(1); // 待审核1
                                    yiyaAuditDiscount.setAuditerOne(sysDept1.getLeader());
                                    yiyaAuditDiscount.setAuditerOneResult(1); //待审核
                                }
                                yiyaAuditDiscountService.insertYiyaAuditDiscount(yiyaAuditDiscount);
                            } else {
                                return AjaxResult.error();
                            }
                        }
                    } else {
                        return AjaxResult.error();
                    }
                } else {
                    return AjaxResult.error();
                }
            }
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @Override
    public List<YiyaBillingMain> selectByIds(List<Long> ids) {
        return yiyaBillingMainMapper.selectByIds(ids);
    }

    @Transactional(rollbackFor=Exception.class)
    public AjaxResult againBilling(AgainBillingRequest againBillingRequest) throws Exception {
        YiyaBillingMain yiyaBillingMainRequest = againBillingRequest.getYiyaBillingMain();
        List<YiyaItemBillingExtend> yiyaItemBillingExtendsRequest = againBillingRequest.getYiyaItemBillingExtends();
        List<YiyaItemBillingExtend> yiyaItemBillingExtendsResponse = yiyaItemBillingService.selectByItemBillingIds(yiyaItemBillingExtendsRequest.stream().map(YiyaItemBillingExtend::getId).collect(Collectors.toList()));
        Map<Long, YiyaItemBillingExtend> itemBillingExtendMap = yiyaItemBillingExtendsRequest.stream().collect(Collectors.toMap(YiyaItemBillingExtend::getId, a -> a, (k1, k2) -> k1));
        if (yiyaBillingMainRequest != null && yiyaBillingMainRequest.getId() > 0) { // 有主单
            YiyaBillingMain yiyaBillingMainResponse = yiyaBillingMainMapper.selectYiyaBillingMainById(yiyaBillingMainRequest.getId());
            YiyaMainClosingEntries yiyaMainClosingEntries = new YiyaMainClosingEntries();
            yiyaMainClosingEntries.setBillingMainId(yiyaBillingMainResponse.getId());
            yiyaMainClosingEntries.setClinicPersonId(yiyaBillingMainResponse.getClinicPersonId());
            yiyaMainClosingEntries.setBillingPerson(againBillingRequest.getBillingPerson());
            yiyaMainClosingEntries.setThisPay(againBillingRequest.getThisPay());
            yiyaMainClosingEntries.setArrearageChange(againBillingRequest.getArrearageChange());
            yiyaMainClosingEntries.setPersonArrearage(againBillingRequest.getPersonArrearage());
            yiyaMainClosingEntries.setBillingRemarks(againBillingRequest.getBillingRemarks());
            yiyaMainClosingEntries.setBillStatus(1); //已开单
            YiyaMainClosingEntries yiyaMainClosingEntriesRequest = new YiyaMainClosingEntries();
            yiyaMainClosingEntriesRequest.setBillingMainId(yiyaBillingMainRequest.getId());
            List<YiyaMainClosingEntries> yiyaMainClosingEntriesResponse = yiyaMainClosingEntriesService.selectYiyaMainClosingEntriesList(yiyaMainClosingEntriesRequest);
            if (yiyaMainClosingEntriesResponse.stream().filter(r -> r.getBillStatus() == 3).collect(Collectors.toList()).size() == yiyaMainClosingEntriesResponse.size()){
                if (againBillingRequest.getType() == 2) { //还欠款
                    if (yiyaBillingMainResponse.getStatus() == 1) {// 主单为未结清 可以再次开单//todo 需要枚举
                        yiyaMainClosingEntries.setType(2);
                        if (againBillingRequest.getThisPay().compareTo(yiyaBillingMainResponse.getOwingAmount()) == 1) { //缴费金额大于欠款
                            return AjaxResult.error("缴费金额大于欠款");
                        }
                        yiyaMainClosingEntries.setDocumentNum(yiyaIDService.createYiyaID(IYiyaIDService.IDType_JIEZHANGDAN,yiyaBillingMainResponse.getClinic())); // 单据号
                        yiyaMainClosingEntriesService.insertYiyaMainClosingEntries(yiyaMainClosingEntries); // 新建缴费单
                        if (yiyaItemBillingExtendsRequest != null && yiyaItemBillingExtendsRequest.size() > 0) {
                            if (yiyaItemBillingExtendsRequest.stream().filter(i -> i.getId() > 0).collect(Collectors.toList()).size() == yiyaItemBillingExtendsRequest.size()) { //所有项目都有项目id
                                yiyaItemBillingExtendsResponse.forEach(r -> {
                                    YiyaItemBillingExtend yiyaItemBillingExtendRequest = itemBillingExtendMap.get(r.getId());
                                    int compareTo = yiyaItemBillingExtendRequest.getThisPay().add(yiyaItemBillingExtendRequest.getStoredDeductPrice()).compareTo(r.getProjectArrearage());
                                    try {
                                        creatTypeClosingWaterRelation(yiyaMainClosingEntries, r, yiyaItemBillingExtendRequest, compareTo);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                });
                            } else {
                                return AjaxResult.error("有项目没有项目id");
                            }
                        } else {
                            return AjaxResult.error("无项目");
                        }
                        } else { //已结清的单 无法再次开单
                            return AjaxResult.error("已结清的单 无法再次开单");
                        }
                    }
                } else {
                    return AjaxResult.error("有结账单据没完结");
                }
            if (againBillingRequest.getType() == 3) { //退款
//                YiyaMainClosingEntries yiyaMainClosingEntriesRequest = new YiyaMainClosingEntries();
                yiyaMainClosingEntriesRequest.setBillingMainId(yiyaBillingMainResponse.getId());
                List<YiyaMainClosingEntries> listYiyaMainClosingEntriesResponse = yiyaMainClosingEntriesService.selectYiyaMainClosingEntriesList(yiyaMainClosingEntriesRequest);
                if (listYiyaMainClosingEntriesResponse.stream().filter(r -> r.getBillStatus() == 3).collect(Collectors.toList()).size() == listYiyaMainClosingEntriesResponse.size()) { // 全部结账单为已结账才能做退款
                    yiyaMainClosingEntries.setType(3); // 退款单
                    if (againBillingRequest.getThisPay().compareTo(yiyaBillingMainResponse.getPayAmount()) == 1) { //退费金额大于缴费金额
                        return AjaxResult.error("退费金额大于缴费金额");
                    }
                    yiyaMainClosingEntries.setDocumentNum(yiyaIDService.createYiyaID(IYiyaIDService.IDType_JIEZHANGDAN,yiyaBillingMainResponse.getClinic())); // 单据号
                    yiyaMainClosingEntriesService.insertYiyaMainClosingEntries(yiyaMainClosingEntries); // 新建退费单
                    if (yiyaItemBillingExtendsRequest != null && yiyaItemBillingExtendsRequest.size() > 0) {
                        List<YiyaItemBillingExtend> ibe = yiyaItemBillingExtendsRequest.stream().filter(i -> i.getId() > 0).collect(Collectors.toList());
                        if (ibe.size() == yiyaItemBillingExtendsRequest.size()) { //所有项目都有项目id
                            List<Long> noRetunIds = yiyaItemBillingExtendsResponse.stream().filter(r -> r.getStatus() != 3).map(YiyaItemBillingExtend::getId).collect(Collectors.toList());//没有退费的项目的id
                            if (noRetunIds.containsAll(ibe.stream().map(YiyaItemBillingExtend::getId).collect(Collectors.toList())) && noRetunIds.size() >= ibe.size()) { //退款项目都不是已退费的项目
                                yiyaItemBillingExtendsResponse.forEach(r -> { //todo 退储值余额许愿处理会员信息表的数据和患者信息的数据
                                    YiyaItemBillingExtend yiyaItemBillingExtendRequest = itemBillingExtendMap.get(r.getId());
                                    int compareTo = yiyaItemBillingExtendRequest.getThisPay().add(yiyaItemBillingExtendRequest.getStoredDeductPrice()).compareTo(r.getProjectReceived());
                                    try {
                                        creatTypeClosingWaterRelation(yiyaMainClosingEntries, r, yiyaItemBillingExtendRequest, compareTo);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                });
                            } else {
                                return AjaxResult.error("有项目无法开退款单");
                            }
                        } else {
                            return AjaxResult.error("有项目没有项目id");
                        }
                    } else {
                        return AjaxResult.error("无项目");
                    }
                } else {
                    return AjaxResult.error("有单据没有结账");
                }
            }
            return AjaxResult.success();
        } else { //无主单
            return AjaxResult.error("无主单");
        }
    }

    private void creatTypeClosingWaterRelation(YiyaMainClosingEntries yiyaMainClosingEntries, YiyaItemBillingExtend r, YiyaItemBillingExtend yiyaItemBillingExtendRequest, int compareTo) throws Exception {
        if (compareTo == -1 || compareTo == 0) { //退费金额小于或者等于已收金额   缴费金额小于或者等于欠款
            if (yiyaItemBillingExtendRequest.getThisPay().add(yiyaItemBillingExtendRequest.getStoredDeductPrice()).compareTo(BigDecimal.ZERO) == 1 || yiyaItemBillingExtendRequest.getThisPay().add(yiyaItemBillingExtendRequest.getStoredDeductPrice()).compareTo(BigDecimal.ZERO) == 0){ // 退费/缴费金额大于0 或者等于0
                YiyaClosingWaterRelation yiyaClosingWaterRelation = new YiyaClosingWaterRelation();
                yiyaClosingWaterRelation.setItemBillingId(r.getId());
                yiyaClosingWaterRelation.setMainClosingEntriesId(yiyaMainClosingEntries.getId());
                yiyaClosingWaterRelation.setStoredDeductPrice(yiyaItemBillingExtendRequest.getStoredDeductPrice());
                yiyaClosingWaterRelation.setThisPay(yiyaItemBillingExtendRequest.getThisPay());
                yiyaClosingWaterRelationService.insertYiyaClosingWaterRelation(yiyaClosingWaterRelation); // 新建项目结账流水项目关联关系
            } else {
                throw new Exception("退费/缴费金额不大于0");
            }
        } else {
            throw new Exception("没生成流水项目对应关系");
        }
    }


    @Override
    public List<?> extendList(ChargeRecordRequest chargeRecordRequest) {
        Long clinicPersonId = chargeRecordRequest.getClinicPersonId();
        if (clinicPersonId != null && clinicPersonId > 0) {
            YiyaBillingMain yiyaBillingMainRequest = new YiyaBillingMain();
            yiyaBillingMainRequest.setClinicPersonId(clinicPersonId);
            yiyaBillingMainRequest.setStatus(chargeRecordRequest.getMainStatus());
            List<YiyaBillingMain> yiyaBillingMains = yiyaBillingMainMapper.selectYiyaBillingMainList(yiyaBillingMainRequest);
            List<ClinicPersonEntriesVo> clinicPersonEntriesVos = new ArrayList<>();
            YiyaMainClosingEntries yiyaMainClosingEntriesRequest = new YiyaMainClosingEntries();
            if (yiyaBillingMains.size() > 0 && chargeRecordRequest.getBillStatus() != null && chargeRecordRequest.getBillStatus() >= 0 && chargeRecordRequest.getType() != null && chargeRecordRequest.getType() >= 0){
                yiyaMainClosingEntriesRequest.setClinicPersonId(clinicPersonId);
                yiyaMainClosingEntriesRequest.setBillStatus(chargeRecordRequest.getBillStatus());
                yiyaMainClosingEntriesRequest.setType(chargeRecordRequest.getType());
                yiyaBillingMains.forEach(bm -> {
                    buildVos(clinicPersonEntriesVos, yiyaMainClosingEntriesRequest, bm);
                });
                return clinicPersonEntriesVos;
            }
            else if (yiyaBillingMains.size() > 0 && chargeRecordRequest.getBillStatus() != null && chargeRecordRequest.getBillStatus() >= 0 ){
                yiyaMainClosingEntriesRequest.setClinicPersonId(clinicPersonId);
                yiyaMainClosingEntriesRequest.setBillStatus(chargeRecordRequest.getBillStatus());
                yiyaBillingMains.forEach(bm -> {
                    buildVos(clinicPersonEntriesVos, yiyaMainClosingEntriesRequest, bm);
                });
                return clinicPersonEntriesVos;
            }
            else if (yiyaBillingMains.size() > 0 && chargeRecordRequest.getType() != null && chargeRecordRequest.getType() >= 0){
                yiyaMainClosingEntriesRequest.setClinicPersonId(clinicPersonId);
                yiyaMainClosingEntriesRequest.setType(chargeRecordRequest.getType());
                yiyaBillingMains.forEach(bm -> {
                    buildVos(clinicPersonEntriesVos, yiyaMainClosingEntriesRequest, bm);
                });
                return clinicPersonEntriesVos;
            }
            else if (yiyaBillingMains.size() > 0) {
                yiyaMainClosingEntriesRequest.setClinicPersonId(clinicPersonId);
                yiyaBillingMains.forEach(bm -> {
                    buildVos(clinicPersonEntriesVos, yiyaMainClosingEntriesRequest, bm);
                });
                return clinicPersonEntriesVos;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    private void buildVos(List<ClinicPersonEntriesVo> clinicPersonEntriesVos, YiyaMainClosingEntries yiyaMainClosingEntriesRequest, YiyaBillingMain bm) {
        yiyaMainClosingEntriesRequest.setBillingMainId(bm.getId());
        ClinicPersonEntriesVo clinicPersonEntriesVo = new ClinicPersonEntriesVo();
        List<ListMainClosingEntriesVo> listOne = yiyaMainClosingEntriesService.extendList(yiyaMainClosingEntriesRequest).getListOne();
        clinicPersonEntriesVo.setMainClosingEntriesVo(listOne.size() > 0 ? listOne.get(0) : null);
        clinicPersonEntriesVo.setListMainClosingEntriesVos(listOne);
        if (clinicPersonEntriesVo != null && listOne.size() >0){
            clinicPersonEntriesVos.add(clinicPersonEntriesVo);
        }
    }
}
